<template>
    <el-dialog v-dialogDrag :title="title" :visible="budgetShow" width="60%" @close="dialogClose"
        :close-on-click-modal="false">
        <el-form ref="budgetForm" :model="formFileds" label-width="auto">
            <el-row :gutter="24">
                <el-col :span="8">
                    <el-form-item label="月份" prop="date">
                        <el-select v-if="this.action == 'add'" v-model="formFileds.date" clearable @change="ajlxShow">
                            <el-option v-for="item in monthOptions" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                        <el-input v-if="this.action == 'edit'" v-model="formFileds.date" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="案件类型" prop="type">
                        <el-select v-if="this.action == 'add'" v-model="formFileds.type" clearable @change="monthShow">
                            <el-option v-for="item in ajlxOptions" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                        <el-input v-if="this.action == 'edit'" v-model="formFileds.type" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="案件数量" prop="yajsl">
                        <el-input type="number" v-model="formFileds.yajsl" @change="getnumber"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="实收金额" prop="yajsr">
                        <el-input type="number" v-model="formFileds.yajsr" @change="getnumber"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="应收金额" prop="yysje">
                        <el-input type="number" v-model="formFileds.yysje"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="平均案件单价" prop="ypjajdj">
                        <el-input v-model="formFileds.ypjajdj" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="成本" prop="yajcb">
                        <el-input type="number" v-model="formFileds.yajcb" @change="getnumber"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="案件毛利" prop="yajml">
                        <el-input v-model="formFileds.yajml" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="毛利率:" prop="yajmll">
                        <el-input v-model="formFileds.yajmll" readonly></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item>
                <el-button type="primary" @click="submitForm()" class="pull-right margin-l-25">确定
                </el-button>
                <el-button @click="dialogClose" class="pull-right">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>
  
<script>
import { addys, updata } from '@/api/financialManagement/budget';
export default {
    //接受父组件
    props: {
        budgetShow: {
            type: Boolean
        },
        record: {
            type: Object
        },
        year: {
            type: String
        },
        action: {
            type: String
        },
        tableData: {
            type: Array
        }
    },
    name: 'BudgetShow',
    data () {
        return {
            title: '',
            formFileds: {
                qyid:Number(localStorage.getItem("companyId"))
                date: '',
                type: '',
                yajsl: '',
                yajsr: '',
                ypjajdj: '',
                yajcb: '',
                yajml: '',
                yajmll: ''
            },
            arrmonth: [],
            monthOptions: [],
            ajlxArr: [],
            ajlxOptions: [],
        };
    },
    methods: {
        getnumber () {
            if (this.formFileds.yajsr && this.formFileds.yajsl) {
                this.formFileds.ypjajdj = (this.formFileds.yajsr / this.formFileds.yajsl).toFixed(2)
            } else {
                this.formFileds.ypjajdj = ''
            }
            if (this.formFileds.yajsr && this.formFileds.yajcb) {
                this.formFileds.yajml = parseFloat(this.formFileds.yajsr) - parseFloat(this.formFileds.yajcb)
            } else {
                this.formFileds.ajml = ''
            }
            if (this.formFileds.yajml && this.formFileds.yajsr) {
                this.formFileds.yajmll = (this.formFileds.yajml / this.formFileds.yajsr * 100).toFixed(2) + '%'
            } else {
                this.formFileds.yajmll = ''
            }
        },
        getMonth () {
            this.arrmonth = []
            let years = this.year.substring(0, 4)
            var value
            for (let i = 1; i < 13; i++) {
                if (i < 10) {
                    value = years + '0' + i
                } else {
                    value = years + i
                }
                this.arrmonth.push(value)
            }
            for (let i = 0; i < this.arrmonth.length; i++) {
                let n = 0
                for (let j = 0; j < this.tableData.length; j++) {
                    if (this.arrmonth[i] == this.tableData[j].date) {
                        n++
                        if (n == 3) {
                            this.arrmonth.splice(i, 1)
                            i--
                            break
                        }
                    }
                }
            }
            this.monthOptions = []
            for (let i = 0; i < this.arrmonth.length; i++) {
                this.monthOptions.push({
                    value: this.arrmonth[i],
                    label: this.arrmonth[i]
                })
            }
        },
        getajlx (arr) {
            this.ajlxOptions = []
            if (arr) {
                for (let i = 0; i < arr.length; i++) {
                    this.ajlxOptions.push({
                        label: arr[i],
                        value: arr[i]
                    })
                }
            }

        },
        ajlxShow () {
            this.ajlxArr = ['个人', '企业机构', '司法机构']
            if (!this.formFileds.date) {
                this.getajlx(this.ajlxArr)
            } else {
                var a
                for (let i = 0; i < this.tableData.length; i++) {
                    if (this.formFileds.date == this.tableData[i].date) {
                        a = this.ajlxArr.indexOf(this.tableData[i].type, 0)
                        if (a >= 0) {
                            this.ajlxArr.splice(a, 1)
                        }
                    }
                }
                this.getajlx(this.ajlxArr)
            }
        },
        monthShow () {
            let years = this.title.substring(0, 4)
            var value
            var monthArr = []
            for (let i = 1; i < 13; i++) {
                if (i < 10) {
                    value = years + '0' + i
                } else {
                    value = years + i
                }
                monthArr.push(value)
            }
            if (!this.formFileds.type) {
                this.getMonth()
            } else {
                var a
                for (let i = 0; i < this.tableData.length; i++) {
                    if (this.formFileds.type == this.tableData[i].type) {
                        a = monthArr.indexOf(this.tableData[i].date, 0)
                        if (a >= 0) {
                            monthArr.splice(a, 1)
                        }
                    }
                }
                this.monthOptions = []
                for (let i = 0; i < monthArr.length; i++) {
                    this.monthOptions.push({
                        label: monthArr[i],
                        value: monthArr[i]
                    })
                }
            }
        },
        dialogClose () {
            this.$emit("dialogClose");
        },
        submitForm () {
            if (this.action == 'add') {
                addys(this.formFileds).then((res) => {
                    if (res) {
                        this.$message.success("添加成功");
                        this.$emit('gettable');
                        this.$emit("dialogClose");
                    }
                })
            } else if (this.action == 'edit') {
                updata(this.formFileds).then((res) => {
                    if (res) {
                        this.$message.success("修改成功");
                        this.$emit('gettable');
                        this.$emit("dialogClose");
                    }
                })
            }
        },
    },
    mounted () {
        if (this.action == 'add') {
            this.getMonth()
            this.ajlxShow()
        } else if (this.action == 'edit') {
            this.formFileds = Object.assign({}, this.record)
        }
        this.title = this.year.substring(0, 7)
    }
};
</script>
  
<style lang="less" scoped>

</style>
  